#ifndef PUZZLESTATE_H
#define PUZZLESTATE_H

#include <vector>
#include <string>
#include <QPoint>

class PuzzleState
{
public:
    PuzzleState(int size = 3);
    PuzzleState(const std::vector<int>& state, int size = 3);
    PuzzleState(const std::vector<std::vector<int>>& state2d);
    PuzzleState(const PuzzleState& other);
    
    // 基本操作
    bool operator==(const PuzzleState& other) const;
    bool operator!=(const PuzzleState& other) const;
    bool operator<(const PuzzleState& other) const;
    PuzzleState& operator=(const PuzzleState& other);
    
    // 状态操作
    std::vector<PuzzleState> getSuccessors() const;
    bool canMove(int direction) const; // 0:上, 1:下, 2:左, 3:右
    PuzzleState move(int direction) const;
    QPoint getBlankPosition() const;
    
    // 启发式函数
    int manhattanDistance(const PuzzleState& goal) const;
    int misplacedTiles(const PuzzleState& goal) const;
    
    // 可解性判断
    bool isSolvable(const PuzzleState& goal) const;
    int getInversionsCount() const;  // 获取逆序数
    
    // 工具函数
    std::string toString() const;
    std::vector<int> getState() const { return state; }
    std::vector<std::vector<int>> getGrid() const;
    int getSize() const { return size; }
    void setState(const std::vector<int>& newState);
    int getValue(int row, int col) const;
    void setValue(int row, int col, int value);
    
    // 静态工具函数
    static PuzzleState getDefaultInitial(int size = 3);
    static PuzzleState getDefaultGoal(int size = 3);
    static PuzzleState generateRandom(int size = 3);
    // 与UI调用保持兼容的别名
    static PuzzleState getDefaultInitialState(int size = 3);
    static PuzzleState getDefaultGoalState(int size = 3);
    static PuzzleState generateRandomSolvableState(int size = 3);

private:
    std::vector<int> state;
    int size;
    
    int getInversions() const;
    int findBlankRow() const;
};

#endif // PUZZLESTATE_H